#
# Copyright (C) [2024] Xingyun Integrated Circuit, Inc.
#
# GreenCode was a private technology asset of Xingyun Integrated Circuit， Inc （Confidential）
# Author: Shawn.Tan
# Date: 2025.10.28
#
# History: Initial Version 2025.10.28
#
#
from gpgpu.EnvGPGPU import EnvGPGPU
from gpgpu.GenThreadGPGPU import GenThreadGPGPU
from base.Sequence import Sequence


class MainSequence(Sequence):
    def generate(self, **kargs):
        if self.getGlobalState("AppRegisterWidth") == 32:
            random_instructions = [
                "ADD##CALM",
                "SRLI#RV32I#CALM",
                "ADDI##CALM",
                "SLLI#RV32I#CALM",
                "LUI##CALM",
            ]
            align = 4
            size = 32
        else:
            random_instructions = [
                "ADDW##CALM",
                "SRLI#RV64I#CALM",
                "ADDI##CALM",
                "SLLI#RV64I#CALM",
                "LUI##CALM",
            ]
            align = 8
            size = 48

        branch_instr = "JALR##CALM"
        for _ in range(10):
            for _ in range(self.random32(0, 5)):
                self.genInstruction(self.choice(random_instructions))

            (opt_value, opt_valid) = self.getOption("FlatMap")

            rand_VA = 0
            if opt_valid:
                rand_VA = self.genVA(
                    Size=size,
                    Align=align,
                    Type="I",
                    Bank="Default",
                    FlatMap=opt_value,
                )
            else:
                rand_VA = self.genVA(Size=size, Align=align, Type="I", Bank="Default")

            self.notice("gen target VA={:#x}".format(rand_VA))
            self.genInstruction(branch_instr, {"BRTarget": rand_VA})


MainSequenceClass = MainSequence
GenThreadClass = GenThreadGPGPU
EnvClass = EnvGPGPU
